package andris.android.mobilarena.widget;

import andris.android.mobilarena.MobilarenaApplication;
import andris.android.mobilarena.R;
import andris.android.mobilarena.activity.MainActivity;
import andris.android.mobilarena.provider.BookmarkListAdapter;
import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.ListFragment;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

public class BookmarkListFragment extends ListFragment {

	private BookmarkListAdapter mAdapter;
	private Vibrator mVibrator;
	private boolean mMode;
	private ActionMode mActionMode;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mAdapter = new BookmarkListAdapter(getActivity(), MobilarenaApplication.articles.getArticles());
		setListAdapter(mAdapter);
		getListView().setOnItemClickListener(mOnItemClickListener);
		getListView().setOnItemLongClickListener(mLongClickListener);
		mMode = false;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return inflater.inflate(R.layout.bookmark_fragment, null);
	}

	private OnItemLongClickListener mLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

			if (mActionMode != null) {
				return false;
			}

			if (mVibrator == null)
				mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
			mVibrator.vibrate(150);
			Toast.makeText(getActivity(), "long press registered on " + MobilarenaApplication.articles.getArticles().get(arg2).getTitle() + ", Mode : edit", Toast.LENGTH_SHORT).show();
			mMode = true;
			
			// cance viewpager scrolling
			((MainActivity)getActivity()).toggleViewPager(false);
			
			mActionMode = getActivity().startActionMode(mActionModeCallback);
			arg1.setSelected(true);
			return true;
		}
	};

	private OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			Toast.makeText(getActivity(), MobilarenaApplication.articles.getArticles().get(arg2).getTitle(), Toast.LENGTH_SHORT).show();
		}

	};

	private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

		// Called when the action mode is created; startActionMode() was called
		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			// Inflate a menu resource providing context menu items
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.context_menu, menu);
			return true;
		}

		// Called each time the action mode is shown. Always called after
		// onCreateActionMode, but
		// may be called multiple times if the mode is invalidated.
		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false; // Return false if nothing is done
		}

		// Called when the user selects a contextual menu item
		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			switch (item.getItemId()) {
			default:
				return false;
			}
		}

		// Called when the user exits the action mode
		@Override
		public void onDestroyActionMode(ActionMode mode) {
			((MainActivity)getActivity()).toggleViewPager(true);
			mActionMode = null;
		}
	};

}
